﻿using CommonTools;
using shop.Bll.daifa;
using shop.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Flurl.Http;
using unvell.ReoGrid;

namespace shop.daifa
{
    public class NeedBuyerInfoBllUI: NeedBuyerInfoBll
    {
        public override ControlBuilder GetUIBuilder()
        {

            return base.GetUIBuilder()
                .UseBoxWidth(136)
                //.AddButton("导入新任务", importNewData, false);
                .AddButton("导入", importData, false)
                .AddButton("下载导入模板", downloadTemplate, false)
                .AddButton("开始查询任务", queryBuyerInfo, false)
                .SetFlowBreak()
                .AddButton("修改状态", ModifyIsOk, false)
                .AddButton("下载报表", downloadRpt , false)
                ;

        }

        ///// <summary>  </summary>
        //private async Task<PageButtonResult> importNewData()
        //{
        //    var form = new DaifaForm();
        //    form.ShowDialog();

        //    return await Task.FromResult( new PageButtonResult(ResultCode.Warning, null, false));
        //}

        /// <summary>  </summary>
        private async Task<PageButtonResult> importData()
        {

            var dt = ExcelHelper.Import();
            if (dt == null)
            {
                return new PageButtonResult(false, "没有导入数据");
            }

            //
            //var batch = DateTime.Now.ToString("yyyy-MM-ddTHHmmss") + Guid.NewGuid().ToString();

            var res = await WaitUI.RunAsync(async () =>
            {
                var list = dt.AsEnumerable()
                 .Select(a => new NeedBuyerInfo()
                 {
                    ShopName = a.Field<string>(0),
                     src_tids= a.Field<string>(1),
                     TradeNo= a.Field<string>(2),
                     Qty = a.Field<string>(3).IsNull(0m),
                     spec_no = a.Field<string>(4),
                     Stock = a.Field<string>(5),
                 })
                 .Where(a => string.IsNullOrEmpty(a.ShopName) == false || string.IsNullOrEmpty(a.src_tids) == false || string.IsNullOrEmpty(a.TradeNo) == false )
                 .ToList();

                var res1 = await base.SaveItem(list);


                return res1;
            });

            if (res.IsOk)
            {
                //修改旧的日期  a是旧数据

                return new PageButtonResult(true, "导入完成", false);
            }
            else
            {
                return new PageButtonResult(true, res.Message);
                //MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }


        }


        /// <summary>  </summary>
        private async Task<PageButtonResult> downloadTemplate()
        {
            var list = (new int[0])
                           .Select(a => new
                           {
                               店铺 = string.Empty,
                               原始单号 = string.Empty,
                               订单编号 = string.Empty,
                               数量= 0,
                               商家编码 = string.Empty,
                               仓库= string.Empty,
                           })
                           .ToList();

            new ExcelExporter()
                .NewSheet(null)
                .AddList(list)
                .EndThenOpen("代发导入模板");


            return await Task.FromResult(new PageButtonResult(false));
        }

        private async Task<PageButtonResult> downloadRpt()
        {

            var pr = base.GetFilterParameter();
            pr.PageNo = 1;
            pr.PageSize = int.MaxValue;
            var list = await WaitUI.RunAsync(async () =>  await this.QueryPage( pr));

            new ExcelExporter()
                .NewSheet("")
                .AddList(list.Data)
                .EndThenOpen("");


            return new PageButtonResult(false, null, false);
        }


        /// <summary> 开始查询买家信息 </summary>
        private async Task<PageButtonResult> queryBuyerInfo()
        {
            //向服务端发送一个消息
            //服务端

            await WaitUI.RunAsync(async () =>
                await myApp.GetClient().Request("Daifa/BeginQueryNeedInfo")
                    .GetAsync()
                    );


            return new PageButtonResult(ResultCode.Warning, "已经开始查询, 请等候.");

        }


        /// <summary> 修改状态 </summary>
        private async Task<PageButtonResult> ModifyIsOk()
        {
            var ids = base.GetSelectedItemsId();
            if(ids.Any() == false)
            {
                return new PageButtonResult(false, "请先勾选需要编辑的项目");
            }

            var grid = base.GetGrid() as Grid;
            var okCol = grid.GetColumnHeader(nameof(NeedBuyerInfo.IsOk));


            foreach (var item in ids)
            {
                await base.ModifyIsOkField(item.Key, false);
                grid[okCol.Index, item.Value] = false;
            }

            return new PageButtonResult(true, "修改完成");

        }


    }
}
